package fon.rm2.client.socket;

import java.net.Socket;

import fon.rm2.client.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SocketActivity extends Activity 
{
	@SuppressWarnings("unused")
	private static final String TAG = "SocketActivity";
	
	Button btnStopSocketActivity, btnConnectToServer, btnSendToServer,
		btnStartListeningForServerMessages, btnStopListeningForServerMessages;
	Socket s = null;
	EditText txtMessageToSend = null;

	ClientThread client = null;
	
	public boolean stopClientThread = false;
	
	public final Handler mHandler = new Handler();
	
	
    public final Runnable mUpdateUIFromClient = new Runnable() 
    {
        public void run() 
        {
            updateUiFromClient();
        }
    };
    
    private void updateUiFromClient() {	
    	Toast.makeText(SocketActivity.this, "From client thread: " + client.currentMessage, Toast.LENGTH_SHORT).show();
    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try 
		{ 
			super.onCreate(savedInstanceState);					
	        setContentView(R.layout.socket);

	        txtMessageToSend = (EditText) findViewById(R.id.txtMessageToSend);
	        txtMessageToSend.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					txtMessageToSend.setText("");
				}
			});
			
			btnStopSocketActivity = (Button) findViewById(R.id.btnStopSocketActivity);
	        btnStopSocketActivity.setOnClickListener(new View.OnClickListener() 
	        {	
				@Override
				public void onClick(View view) {
					try 
					{
						SocketActivity.this.stopClientThread = true;
						client = null;
						SocketActivity.this.btnStartListeningForServerMessages.setEnabled(false);
						SocketActivity.this.btnStopListeningForServerMessages.setEnabled(false);
						SocketActivity.this.btnSendToServer.setEnabled(false);
						SocketActivity.this.txtMessageToSend.setEnabled(false);
						SocketActivity.this.txtMessageToSend.setClickable(false);
						finish();
					} catch (Exception e) 
					{
						Toast.makeText(SocketActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
	        });
	        
	        btnConnectToServer = (Button) findViewById(R.id.btnConnectToServer);
	        btnConnectToServer.setOnClickListener(new View.OnClickListener() 
	        {	
				@Override
				public void onClick(View view) 
				{
					try
					{
						client = new ClientThread(SocketActivity.this, "192.168.1.2", 8090);
						client.start();
						
						SocketActivity.this.btnConnectToServer.setEnabled(false);
						SocketActivity.this.btnStartListeningForServerMessages.setEnabled(true);
						SocketActivity.this.btnSendToServer.setEnabled(true);
						SocketActivity.this.txtMessageToSend.setEnabled(true);
						SocketActivity.this.txtMessageToSend.setClickable(true);
					}
					catch(Exception e)
					{
						Toast.makeText(SocketActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
					}
					
				}
	        });
	        
	        btnSendToServer = (Button) findViewById(R.id.btnSendToServer);
	        btnSendToServer.setOnClickListener(new View.OnClickListener() 
	        {	
				@Override
				public void onClick(View view) {
					try
					{
						Toast.makeText(SocketActivity.this, R.string.msg_sending, Toast.LENGTH_SHORT).show();
						SocketActivity.this.client.SendMessageToServer(txtMessageToSend.getText().toString());
					} 
					catch (Exception e) 
					{
						Toast.makeText(SocketActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
					}
				}
	        });
			
	        btnStartListeningForServerMessages = (Button) findViewById(R.id.btnStartListeningForServerMessages);
	        btnStartListeningForServerMessages.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					SocketActivity.this.btnStartListeningForServerMessages.setEnabled(false);
					SocketActivity.this.btnStopListeningForServerMessages.setEnabled(true);
					client.ResumeClient();
				}
			});
	        
	        btnStopListeningForServerMessages = (Button) findViewById(R.id.btnStopListeningForServerMessages);
	        btnStopListeningForServerMessages.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					SocketActivity.this.btnStopListeningForServerMessages.setEnabled(false);
					SocketActivity.this.btnStartListeningForServerMessages.setEnabled(true);
					client.PauseClient();
				}
			});
	        
	        this.btnStartListeningForServerMessages.setEnabled(false);
	        this.btnStopListeningForServerMessages.setEnabled(false);
	        this.btnSendToServer.setEnabled(false);
	        this.txtMessageToSend.setEnabled(false);
	        this.txtMessageToSend.setClickable(false);
	        
		}
		catch(Exception ex)
		{
			Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
}